
import numpy as np
import cv2 as cv
from skimage import io, transform
import os
import os.path
import glob
import copy

img_type = ".png"
row = 28
col = 28 
c   = 1

def read_img(path):
    cate = [x for x in os.listdir(path) if os.path.isdir(path + x)] 
    cate.sort()
    
    print('------------', cate)
    imgs = []
    labels = []
    np_label = np.zeros((1, len(cate)), dtype=np.int)
    org_label = list(np_label[0])
    count = 0
    for idx, folder in enumerate(cate):
        for im in glob.glob(path + folder + '/*' + img_type):
            count += 1
            print('reading the image %d: %s' % (count,im))
            make_label = copy.deepcopy(org_label)
            make_label[idx] = 1

            labels.append(make_label)

            img = cv.imread(im)
            img = transform.resize(img, (row, col, c), mode='constant')
            imgs.append(img)
    return np.asarray(imgs, np.float32), np.asarray(labels, np.int32)


		
if __name__ == "__main__":

	path = "../test_img"

	path = os.path.abspath(path)
	print(path)
	if not os.path.exists(path):
		print("*** path is not exist")
		exit(-1)
	data, label = read_img(path + "/")
	cv.imshow('test', data[0])
	cv.waitKey(0)
	cv.destroyAllWindows()











